package com.yxhl.zoume.core.expresscar.presenter;

import android.content.Context;

import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.MpvModelRequest;
import com.yxhl.protobuf.MpvModelResponse;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.expresscar.view.QueryPriceAndDistanceView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.car.expresscar.QueryPriceAndDistanceUseCase;
import com.yxhl.zoume.utils.TimeUtils;

import javax.inject.Inject;

/**
 * Author：zhongshan   
 * Date：2016/12/14 17:39      
 * Description：
 */
public class SingleWayQueryPresenter extends BasePresenter {

    @Inject
    QueryPriceAndDistanceUseCase mQueryPriceAndDistanceUseCase;
    private QueryPriceAndDistanceView mSingleWayQueryView;
    /**
     * 出发点 经度
     */
    private String lng;
    private String lat;
    /**
     * 到达地 经度
     */
    private String endLng;
    private String endLat;

    private String vehicleType;
    private String departTime;


    @Inject
    public SingleWayQueryPresenter(Context context) {
        super(context);
    }


    public void queryPriceAndDistance(OrderBizType orderBizType) {

        final MpvModelRequest.Builder mpvModelRequestBuilder = MpvModelRequest.newBuilder()
                .setCommonRequest(getCommonReqParams())
                .setBindType(orderBizType)
                .setLng(lng)
                .setLat(lat)
                .setElat(endLat)
                .setElng(endLng)
                .setVehicleType(vehicleType)
                .setGmtDepart(departTime);

        mQueryPriceAndDistanceUseCase
                .execute(mpvModelRequestBuilder.build(),new ZMSubscriber<MpvModelResponse>() {
                    @Override
                    public void onNext(MpvModelResponse mpvModelResponse) {
                        super.onNext(mpvModelResponse);
                        CommonRespParams commonResponse = mpvModelResponse.getCommonResponse();
                        if (commonResponse != null) {
                            String isSucc = commonResponse.getIsSucc();
                            if ("true".equalsIgnoreCase(isSucc)) {
                                String desc = mpvModelResponse.getDesc();
                                String totalFee = mpvModelResponse.getTotalFee();
                                String runKm = mpvModelResponse.getRunKm();
                                String runMin = mpvModelResponse.getRunMin();
                                mSingleWayQueryView.renderQueryPriceAndDistanceOnSuccess(runKm,runMin ,totalFee, desc);
                            } else {
                                String resultMsg = commonResponse.getResultMsg();
                                mSingleWayQueryView.renderQueryPriceAndDistanceOnFailure(resultMsg);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSingleWayQueryView.renderQueryPriceAndDistanceOnError(e.toString());
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }


    @Override
    public void attachView(BaseView view) {
        if (view instanceof QueryPriceAndDistanceView) {
            mSingleWayQueryView = (QueryPriceAndDistanceView) view;
        }
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public void setEndLng(String endLng) {
        this.endLng = endLng;
    }

    public void setEndLat(String endLat) {
        this.endLat = endLat;
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
    }


    public void handleZMTimePickerSelectResult(String options1, String options2, String options3) {
        StringBuilder sb = new StringBuilder();
        if ("现在".equals(options1)) {
            sb.append(TimeUtils.exchangeToTime(System.currentTimeMillis()));
        } else {
            sb.append(TimeUtils.getCurrentYear());
            sb.append("-");
            sb.append(options1.replace("月", "-").replace("日", " "));
            sb.append(options2.replace("点", ":"));
            sb.append(options3.replace("分", ""));
            sb.append(":00");
        }
        departTime = sb.toString();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mQueryPriceAndDistanceUseCase.unsubscribe();
    }
}
